package com.ckzp.jfinal.system;

import com.ckzp.core.ConfigUtil;
import com.ckzp.core.Results;
import com.ckzp.core.util.CookieUtil;
import com.ckzp.jfinal.system.po.WebUser;
import com.ckzp.core.util.JwtUtil;
import com.ckzp.core.util.StringUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by 51594 on 2022/7/27.
 */
public class WebSite {

    public static String COOKIE_ID = ConfigUtil.getProperty("COOKIE_ID", "JWTCOOKIE");

    /**
     * 取得登陆用户所有信息。
     *
     * @param request
     * @return
     */
    public static WebUser getUserInfo(HttpServletRequest request) {
        HttpSession session = request.getSession();
        Object o = session.getAttribute("USERINFO");
        if (o != null && o instanceof WebUser) {
            return (WebUser) o;
        } else {
            return new WebUser();
        }
    }

    /**
     * 取得登陆用户所有信息。
     *
     * @param response
     * @return
     */
    public static void setJwt(HttpServletResponse response, String jwt) {
        CookieUtil.setCookie(response, COOKIE_ID, jwt);
    }

    /**
     * 取得登陆用户所有信息。
     *
     * @param response
     * @return
     */
    public static void removeJwt(HttpServletRequest request, HttpServletResponse response) {
        CookieUtil.removeCookie(request, response, COOKIE_ID);
    }

    /**
     * 取得登陆用户所有信息。
     *
     * @param request
     * @return
     */
    public static String getJwt(HttpServletRequest request) {
        return CookieUtil.getCookie(request, COOKIE_ID);
    }

    /**
     * 取得登陆用户所有信息。
     *
     * @param request
     */
    public static void emptyWebSite(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.setAttribute("USERINFO", null);
    }

    /**
     * 取得用户姓名
     *
     * @param request
     * @return
     */

    public static String getEmpName(HttpServletRequest request) {
        WebUser u = getUserInfo(request);
        return u.getEmpName() == null ? "访客" : u.getEmpName();
    }

    /**
     * 取得用户ID
     *
     * @param request
     * @return
     */
    public static String getEmpId(HttpServletRequest request) {
        WebUser u = getUserInfo(request);
        return u.getEmpID() == null ? "" : u.getEmpID();
    }

    /**
     * 取得用户登陆ID
     *
     * @param request
     * @return
     */
    public static String getLoginId(HttpServletRequest request) {
        WebUser u = getUserInfo(request);
        return u.getLoginID() == null ? "guest" : u.getLoginID();
    }

    /**
     * 取得用户所在部门编号
     *
     * @param request
     * @return
     */
    public static String getDeptId(HttpServletRequest request) {
        WebUser u = getUserInfo(request);
        return u.getDeptid() == null ? "" : u.getDeptid();
    }

    /**
     * 取得用户姓别
     *
     * @param request
     * @return
     */
    public static int getUserSex(HttpServletRequest request) {
        WebUser u = getUserInfo(request);
        return Integer.parseInt(StringUtil.getString(u.getSex(), "1"));
    }

    /**
     * ȡ���û����ڲ�������
     *
     * @param request
     * @return
     */
    public static String getDeptName(HttpServletRequest request) {
        WebUser u = getUserInfo(request);
        return u.getDeptName() == null ? "" : u.getDeptName();
    }


    /**
     * 取得用户类型
     *
     * @param request
     * @return
     */
    public static String getUserType(HttpServletRequest request) {
        WebUser u = getUserInfo(request);
        return u.getUser_type() == null ? "" : u.getUser_type();
    }


    /**
     * 取得用户登陆日期时间
     *
     * @param request
     * @return
     */
    public static String getLoginDateTime(HttpServletRequest request) {
        WebUser u = getUserInfo(request);
        Date loginDate = u.getLoginDate();
        return new SimpleDateFormat("yyyy年MM月dd日 HH时mm分").format(loginDate);
    }

    /**
     * 取得用户登陆日期
     *
     * @param request
     * @return
     */
    public static String getLoginDate(HttpServletRequest request) {
        WebUser u = getUserInfo(request);
        Date loginDate = u.getLoginDate();
        return new SimpleDateFormat("yyyy年MM月dd日").format(loginDate);
    }

    /**
     * 取得用户登陆日期
     *
     * @param request
     * @return
     */
    public static String getRegDate(HttpServletRequest request) {
        WebUser u = getUserInfo(request);
        Date loginDate = u.getLoginDate();
        return new SimpleDateFormat("yyyy-MM-dd").format(loginDate);
    }

    /**
     * 取得用户登陆时间
     *
     * @param request
     * @return
     */
    public static String getLoginTime(HttpServletRequest request) {
        WebUser u = getUserInfo(request);
        Date loginDate = u.getLoginDate();
        return new SimpleDateFormat("HH时mm分").format(loginDate);
    }

    /**
     * 取得用户在线时间长
     *
     * @param request
     * @return
     */
    public static String getOnlineTime(HttpServletRequest request) {
        WebUser u = getUserInfo(request);
        long d = (new Date()).getTime() - u.getLoginDate().getTime();
        long h = d / (60 * 60 * 1000);
        d = d - h * 60 * 60 * 1000;
        long m = d / (60 * 1000);
        d = d - m * 60 * 1000;
        long s = d / 1000;
        return h + "时" + m + "分" + s + "秒";
    }

    /**
     * 取得角色列表
     *
     * @param request
     * @return
     */
    public static String[] getRoleID(HttpServletRequest request) {
        WebUser u = getUserInfo(request);
        return u.getRoles();
    }

    /**
     * 返回角色的String
     *
     * @param request
     * @return
     */
    public static String getRoleIdStr(HttpServletRequest request) {
        return JwtUtil.getJwtInfo(getJwt(request)).getRoles();
    }

    /**
     * 返回角色的String
     *
     * @param request
     * @return
     */
    public static String getPostIdStr(HttpServletRequest request) {
        return JwtUtil.getJwtInfo(getJwt(request)).getPosts();
    }

    /**
     * 判断用户是否属于这个角色
     *
     * @param request
     * @param roleid
     * @return
     */
    public static boolean isInRole(HttpServletRequest request, String roleid) {
        String s = getRoleIdStr(request);
        String[] ss = StringUtil.split(roleid, ",");
        for (int k = 0; k < ss.length; k++) {
            if (s.indexOf(ss[k]) > -1) {
                return true;
            }
        }
        return false;
    }


    /**
     * 判断用户是否属于这个岗位
     *
     * @param request
     * @param postid
     * @return
     */
    public static boolean isInPost(HttpServletRequest request, int postid) {
        String s = getPostIdStr(request);
        if (s.indexOf("|" + postid + "|") > -1) {
            return true;
        }
        return false;
    }

    /**
     * 判断用户是否属于这个角色
     *
     * @param request
     * @param pass_roles
     * @return
     */
    public static boolean isInRoleJwt(HttpServletRequest request, String pass_roles) {
        String jwt = WebSite.getJwt(request);
        boolean has_roles = false;
        Results results = JwtUtil.validateJWT(jwt);
        pass_roles = "|" + pass_roles + "|";
        if (results.getCode() == 0) {
            JwtUtil.JwtSubject js = results.getObject(JwtUtil.JwtSubject.class);
            String user_roles = js.getRoles();
            String[] user_roless = StringUtil.split(user_roles, "|");
            for (int k = 0; k < user_roless.length; k++) {
                String role = user_roless[k];
                if (role != null && role.length() > 0 && pass_roles.indexOf("|" + role + "|") > -1) {
                    has_roles = true;
                    break;
                }
            }
        }
        return has_roles;
    }
}